﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LX.Core.Validation;
using S4Store.Persistent;
using S4Store.Domain.CommonSellerKpi;

namespace S4Store.Service.Valid.SellerKpiValid
{
    public class SellerKpiMonthConfigSave<T> : ValidExpression<T> where T : CommonSellerKpiMonthConfig
    {
        private S4StoreDataContext PersistentContext = new S4StoreDataContext();

        public override void ValidEntity(ValidationResult result, T entity)
        {
            if (entity.Month == DateTime.MinValue)
            {
                PushError(result, "日期不正确");
            }
            if (entity.StoreId == 0)
            {
                PushError(result, "店不正确");
            }

            if (PersistentContext.Set<T>().AsQueryable().Any(t => t.StoreId == entity.StoreId&&t.Month.Year==entity.Month.Year&&t.Month.Month==entity.Month.Month&&t.Id!=entity.Id))
            {
                PushError(result, "这个月已经有一条计划记录了");
            }
        }

    }
}
